package com.ivy.conn;


import com.ivy.entity.School;
import com.ivy.entity.TContent;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Created by Administrator on 2018/1/13.
 */

public class TContentConn {
    private static String result;
    private static HttpURLConnection con;
    private static OutputStream out;

    public static void main(String[] args) {
    	String coursepath="http://1.85.55.152/ZNPK/TeacherKBFB_rpt.aspx";
    	String referer="http://1.85.55.152/ZNPK/TeacherKBFB.aspx";
    	School school = new School();
    	school.setCoursepath(coursepath);
    	school.setReferer(referer);
    	
    	
    	String teacherid = "0000374";
    	String code = "f4jn";
    	String cookie = "ASP.NET_SessionId=c3fzmsicuzj0cbej4qulxvfb";
    	getTContent(teacherid, code, cookie);
    	
    }
    private static String getTContentData(String teacherid , String code, String cookie)  {
        String yearId ="20170";
        String path = "http://1.85.55.152/ZNPK/TeacherKBFB_rpt.aspx";
        String referer="http://1.85.55.152/ZNPK/TeacherKBFB.aspx";
        String params="Sel_XNXQ="+yearId+"&Sel_JS="+teacherid+"&type=1&txt_yzm="+code;

        try{
        	 System.out.println(path+"   ===="+cookie);
            URL url = new URL(path);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Cookie", cookie);
            con.setRequestProperty("Referer", referer);
            OutputStream out = con.getOutputStream();
            out.write(params.getBytes());
            out.flush();
            InputStream in = con.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while((len=in.read(buf))!=-1){
                bout.write(buf, 0, len);
            }
            byte[] buf1 = bout.toByteArray();
            String result = new String(buf1,"utf8");

            bout.close();
            in.close();
            out.close();
            con.disconnect();
            return result;
        }catch (Exception e){
            e.printStackTrace();
           throw new RuntimeException();
        }
    }

    public static List getTContent(String teacherid , String code, String cookie) {
        String temp = getTContentData( teacherid , code, cookie);
        List<TContent> contents = new ArrayList<TContent>();
        if(temp!=null&&temp.indexOf("验证码错误")>0){
        	 TContent content = new TContent();
            
            
             content.setInfo("valierr");
             contents.add(content);
        }
        System.out.println(temp);
        //String result = temp.substring(temp.indexOf("<tr ><td align=center width='5%' rowspan='2' valign=middle >"), temp.lastIndexOf("<TABLE name=theExportData WIDTH=100%"));
        
        Document doc= Jsoup.parse(temp);
        Elements nodes= doc.select("td[valign=top]");
        int x = 0;
        int y = 0;
        for(Element n:nodes) {
            if(y == 7) {
                System.out.println("..........................");
                x+=1;
                y=0;
            }

            TContent content = new TContent();
            content.setInfo(n.text());
            content.setTime(x);
            content.setWeekday(y);
            content.setTid(teacherid);
            System.out.println(n.text()+"========="+x +"========="+y);
            contents.add(content);
            y += 1;
        }
        return contents;
    }

}
